package com.sondon.mayi.adapter;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.Cart;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.ImageLoaderUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.MyAlertDialog;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.adapter
 * @Class : CartList_Item.java
 * @Company 广州讯动网络科技有限公司
 * @Author : 蔡文锋
 * @DateTime：2015-6-18 下午2:21:48
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 购物车列表项 }
 */
@EViewGroup(R.layout.cartlist_item)
public class CartList_Item extends LinearLayout {
	private Context context;
	@ViewById
	CheckBox cartlist_item_checkbox;// 选择
	@ViewById
	TextView cartlist_item_name;// 水果名
	@ViewById
	TextView cartlist_item_price;// 单项价格
	@ViewById
	TextView cartlist_item_oneprice;//单价(会员价)
	@ViewById
	TextView cartlist_item_num;// 数量
	@ViewById
	ImageView cartlist_item_img;//图片
	@ViewById
	ImageView cartlist_item_del;// 删除
	@ViewById
	ImageView cartlist_item_minus;// 减
	@ViewById
	ImageView cartlist_item_plus;// 加
	@ViewById
	LinearLayout cartlist_item_layout;//layout
	

	public double num=0.00;//数量
	public double group_price=0.00;//会员价
	
	private String cartlist_itemId="";//ID
	private CartList_ItemChanageListener mylistener;//购物车列表改变监听事件
	private int position;//ListView中位置
	
	public CartList_Item(Context context) {
		super(context);
		this.context = context;
	}

	// 绑定数据
	public void bindData(Cart cart,CartList_ItemChanageListener listener,boolean isAllChecked,int position) {
		
		num=Float.valueOf(cart.getNum());//数量
		group_price =Float.valueOf(cart.getGoods_info().getPrice());//会员价
		cartlist_itemId=cart.getId();//ID
				
		cartlist_item_name.setText(cart.getGoods_info().getName());//水果名
		cartlist_item_num.setText(cart.getNum());//数量
		cartlist_item_price.setText(""+group_price*num);//单项总价,目前不显示
		cartlist_item_oneprice.setText("￥ "+cart.getGoods_info().getPrice());//单价
		ImageLoaderUtil.SetImgView(cart.getGoods_info().getImg(), cartlist_item_img);//加载图片
		
		this.mylistener=listener;
		this.position=position;
		
		//设置checkbox监听事件
		cartlist_item_checkbox.setOnCheckedChangeListener(new MyCheckedListener(this.mylistener));
		
		//设置选择状态
		cartlist_item_checkbox.setChecked(isAllChecked);
	}
	
	
	// 监听事件
	@Click({ R.id.cartlist_item_minus, R.id.cartlist_item_plus,
			R.id.cartlist_item_del })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cartlist_item_minus://减
			if(num>0){
				num--;
				if(num==0){
//					cartlist_item_minus.setEnabled(false);
					cartlist_item_minus.setVisibility(View.INVISIBLE);
				}
				//是否被选中
				if(cartlist_item_checkbox.isChecked()){
					//总价减一个会员价
					this.mylistener.ItemNumChanage(-(group_price*1),position,num,cartlist_item_checkbox.isChecked());
				}
			}
			changeNum(num);
			
			break;
		case R.id.cartlist_item_plus://加
			num++;
			if(num>0){
//				cartlist_item_minus.setEnabled(true);
				cartlist_item_minus.setVisibility(View.VISIBLE);
				
				//是否被选中
				if(cartlist_item_checkbox.isChecked()){
					//总价加一个会员价
					this.mylistener.ItemNumChanage(group_price*1,position,num,cartlist_item_checkbox.isChecked());
				}
				
			}
			changeNum(num);
		
			break;
		case R.id.cartlist_item_del://删除item
			MyAlertDialog.show(this.context, "购物车", "是否从购物车里移除: \n"+cartlist_item_name.getText().toString().trim()+" ?",
					"NO","YES");
			//左边按钮监听事件
			OnClickListener	 leftListener=new OnClickListener() {
								
								@Override
								public void onClick(View arg0) {
									if(MyAlertDialog.getMydialog().isShowing()){
										MyAlertDialog.getMydialog().dismiss();
									}
								}
							};
							
			//设置监听事件
			MyAlertDialog.setLeftRightListener(leftListener, new myRightListener(this.mylistener,this.position));
			break;
		default:
			break;
		}
	}
	
	//右边按钮监听事件
	class myRightListener implements OnClickListener{
		private CartList_ItemChanageListener mylistener;
		private int position;
		public myRightListener(CartList_ItemChanageListener listener,int position) {
			this.mylistener=listener;
			this.position=position;
		}
		@Override
		public void onClick(View v) {
			if(MyAlertDialog.getMydialog().isShowing()){
				MyAlertDialog.getMydialog().dismiss();
				
				String ukey=context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).getString("Ukey", "");
				LogUtil.e("CartList_Item", "cartlist_itemId  :"+cartlist_itemId);
				String url=URLConfigs.DelCart_ukey_cartid+ukey+"&id="+cartlist_itemId;
				AsyncHttpUtil.get(url, null, new delItemResponse(this.mylistener,this.position));
			}
		}
		
	}

	/*
	 * 改变num时的处理
	 */
	public void changeNum(double num){
		cartlist_item_num.setText(""+num);
		cartlist_item_price.setText(""+group_price*num);
	}
	
	/*
	 * checkbox监听事件
	 */
	class MyCheckedListener implements OnCheckedChangeListener{
		public CartList_ItemChanageListener mylistener;
		
		public MyCheckedListener(CartList_ItemChanageListener listener) {
			this.mylistener=listener;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			float item_total=0;
			if(isChecked){
				 item_total=Float.valueOf(cartlist_item_price.getText().toString().trim());
			}else{
				 item_total=-Float.valueOf(cartlist_item_price.getText().toString().trim());
			}
			//item 是否被选择  num数量
			this.mylistener.ItemNumChanage(item_total,position,num,isChecked);
		}
	}
	
	
	//删除Item的Http请求回调函数
	class delItemResponse extends TextHttpResponseHandler {
		private CartList_ItemChanageListener mylistener;
		private int position;
		public delItemResponse(CartList_ItemChanageListener mylistener,int position) {
			this.mylistener=mylistener;
			this.position=position;
		}
		
		@Override
		public void onSuccess(int status, Header[] arg1, String json) {
			if(status==200){
				LogUtil.e("CartList_Item", "del_item  json:"+json);
				RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
				if(entity.isStatus()){//后台删除成功
					ToastUtils.show(context, entity.getMsg());
					//隐藏掉删除了的item
					this.mylistener.ItemDelChanage(this.position);
				}else{
					ToastUtils.show(context, entity.getMsg());
				}
			}else{
				ToastUtils.show(context, R.string.network_unavailable);
			}
			
		}
		
		@Override
		public void onFailure(int status, Header[] arg1, String str, Throwable arg3) {
			ToastUtils.show(context, R.string.network_unavailable);
		}
	}
}
